<template>
	<view class="page b-f">
		<view class="search_top">
			<view class="search_input_view">
				<image class="search_icon" :src="imgPrefix('/static/image/icon/search_icon.png')"></image>
				<input
					class="search_input"
					@confirm="getTSearchList()"
					placeholder-class="search_input_placeholder"
					placeholder="输入关键词进行资讯查找"
					v-model="searchValue"
					type="text"
				/>
			</view>
		</view>
		<view class="main">
		<notice-list :list="infomationsList" :keyWord="searchValue" @bind-notice-view="bindArticleDetail" />
		<load-more :status="loasStatus"></load-more>
		</view>
	</view>
</template>

<script>
import noticeList from '@/components/notice-list/index.vue'; //资讯列表
import informationBar from '@/components/information-bar/index.vue'; // 资讯标题栏
import loadMore from '@/components/loadmore/index.vue';

import { comPageMixinFun } from '@/static/mixins/comPageCommon.js';
import { commonMixinFun } from '@/static/mixins/common.js';

import API from '@/static/api/index.js';
const app = getApp();
export default {
	components: {
		informationBar,
		noticeList,
		loadMore
	},
	mixins: [commonMixinFun, comPageMixinFun],
	data() {
		return {
			loasStatus: 'empty',
			langeIndex: 0,
			infomationsList: [],
			searchValue:'',
			page: 1,
			size: 10,
			totals: 0,
			isRequest: false,
			historyList:[]
		};
	},
	onLoad(option) {
		this.searchValue = decodeURIComponent(option.value)
	},
	onShow() {
		this.historyList = app.globalData.searchHistory||[];
		this.getTSearchList();
	},
	methods: {
		getTSearchList() {
			let { searchValue, page, size, isRequest, infomationsList, loasStatus,historyList } = this;
			if (!isRequest) {
				this.isRequest = true;
				if (loasStatus != 'nomore') {
					loasStatus = 'loading';
				}
				app.globalData.searchHistory = [searchValue,...new Set(historyList)]; // 追加搜索记录
				API.getTSearchList({search_name:searchValue, page:page, size:size})
					.then(({ code, data, msg }) => {
						if (code == 1) {
							app._log(code, data, msg);
							let tempArr = (page == 1 ? [] : infomationsList).concat(data.infomations);
							if (tempArr.length > 0) {
								this.totals = data.total;
								this.page += data.total >= tempArr.length ? 0 : 1;
							}
							this.infomationsList=tempArr
							this.loasStatus = tempArr.length == 0 ? 'empty' : data.total >= tempArr.length ? 'nomore' : 'getmore';
						} else{
							this.loasStatus = code==405&&page==1?'empty':'nomore';
							if(code==405&&page==1){
								this.infomationsList=[]
							}else{
								this.showToast(msg || '请求失败');
							}
						}
						this.isRequest = false;
					})
					.catch(err => {
						this.isRequest = false;
						this.loasStatus = 'error';
						this.showToast(err.msg || '加载失败');
						this.infomationsList=[]
					});
			}
		},
		// 点击文章
		bindArticleDetail(index) {
			this.pageTo('/pages/teacher/article_details/index',{id:this.infomationsList[index].infomation_id,index:0})
		},
	},
	onReachBottom() {
		this.getTSearchList();
	}
};
</script>

<style lang="scss" scoped>
.page > .title {
	width: 750rpx;
	line-height: 98rpx;
}
.search_top {
	/* #ifndef APP-PLUS-NVUE */
	position: -webkit-sticky;
	/* #endif */
	position: sticky;
	// top: var(--window-top);
	z-index: 99;
	top: 0;
	width: 750rpx;
	height: 120rpx;
	background-color: #fff;
	padding-left: 48rpx;
	.search_input_view {
		width: 654rpx;
		height: 72rpx;
		background-color: #f4f4f4;
		border-radius: 36rpx;
		display: flex;
		align-items: center;
		padding: 0 22rpx;
		overflow: hidden;
		.search_icon {
			width: 26rpx;
			height: 24rpx;
			margin-right: 12rpx;
		}
		.search_input {
			font-size: 26rpx;
			flex: 1;
			&_placeholder {
				color: #cccccc;
			}
		}
	}
}
.main {
	margin-top: 16rpx;
	background-color: #fff;
}
</style>
